/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.io.File;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.core.util.uuid.UUIDGenerator;
import cn.qidisoft.edu.hzjt.constants.Constant;
import cn.qidisoft.edu.hzjt.dao.CommunityAmateurWorkStudentAttMapper;
import cn.qidisoft.edu.hzjt.dao.CommunityAmateurWorkStudentMapper;
import cn.qidisoft.edu.hzjt.model.CommunityAmateurWorkStudent;
import cn.qidisoft.edu.hzjt.model.CommunityAmateurWorkStudentAtt;
import cn.qidisoft.edu.hzjt.service.ICommunityAmateurWorkStudentService;
import cn.qidisoft.edu.hzjt.utils.FileTool;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * 
 * @author Administrator
 * @version $Id: BjxxClassMaterialProjectServiceImpl.java, v 0.1 2018年10月29日 下午1:14:36 hhl Exp $
 */
@Service("communityAmateurWorkStudentService")
public class CommunityAmateurWorkStudentServiceImpl implements ICommunityAmateurWorkStudentService {

    protected static final LoggerAdapter         LOGGER = LoggerAdapterFactory
        .getLogger(LogType.HZJT_LOGGER.val);

    @Autowired
    private CommunityAmateurWorkStudentMapper    communityAmateurWorkStudentMapper;

    @Autowired
    private CommunityAmateurWorkStudentAttMapper communityAmateurWorkStudentAttMapper;

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryList(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public List<CommunityAmateurWorkStudent> queryList(CommunityAmateurWorkStudent data) {
        try {
            return communityAmateurWorkStudentMapper.queryList(data);
        } catch (Exception e) {
            LOGGER.error("queryList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryByPK(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public CommunityAmateurWorkStudent queryByPK(CommunityAmateurWorkStudent data) {
        try {
            return communityAmateurWorkStudentMapper.queryByPK(data);
        } catch (Exception e) {
            LOGGER.error("queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#add(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public String add(CommunityAmateurWorkStudent data) {
        String uuid = UUIDGenerator.genReqID();
        try {
            communityAmateurWorkStudentMapper.add(data);

        } catch (Exception e) {
            LOGGER.error("add [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
        return uuid;
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#update(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void update(CommunityAmateurWorkStudent data) {
        try {

            communityAmateurWorkStudentMapper.update(data);

        } catch (Exception e) {
            LOGGER.error("update [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#delete(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void delete(CommunityAmateurWorkStudent data) {
        try {
            for (String s : data.getIds()) {
                CommunityAmateurWorkStudentAtt data2 = new CommunityAmateurWorkStudentAtt();
                data2.setParentid(Integer.valueOf(s));
                List<CommunityAmateurWorkStudentAtt> communityAmateurWorkStudentAttList = communityAmateurWorkStudentAttMapper
                    .queryList(data2);
                if (communityAmateurWorkStudentAttList != null
                    && !communityAmateurWorkStudentAttList.isEmpty()) {
                    for (CommunityAmateurWorkStudentAtt r : communityAmateurWorkStudentAttList) {
                        String upload = Constant.UPLOADPATH;
                        int idlength = r.getFileid().lastIndexOf("");
                        int pos = r.getFilepath().lastIndexOf(".");
                        String filepath = r.getFilepath().substring(0, pos - idlength - 1);
                        File file = new File(upload + File.separator + filepath);
                        /* if (file.isDirectory()) {//判断file是否是文件目录 若是返回TRUE  
                            String name[] = file.list();//name存储file文件夹中的文件名  
                            for (int i = 0; i < name.length; i++) {
                                File f = new File(upload + File.separator + filepath, name[i]);//此时就可得到文件夹中的文件  
                                f.delete();//删除文件  
                            }
                            FileUtils.forceDelete(new File(upload + File.separator + filepath)); //最后删除文件目录
                        }*/
                        FileTool.deleteFile(file);
                    }
                }
                //删除附件
                CommunityAmateurWorkStudentAtt communityAmateurWorkStudentAtt = new CommunityAmateurWorkStudentAtt();
                communityAmateurWorkStudentAtt.setParentid(Integer.valueOf(s));
                communityAmateurWorkStudentAttMapper.delete(communityAmateurWorkStudentAtt);
                //删除主表
                data.setXszyid(Integer.valueOf(s));
                communityAmateurWorkStudentMapper.delete(data);
            }

        } catch (Exception e) {
            LOGGER.error("delete [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryPageList(cn.qidisoft.microClass.resource.model.basis.Catalog, cn.qidisoft.core.common.pagination.Page)
     */
    @Override
    public PaginationBean<CommunityAmateurWorkStudent> queryPageList(CommunityAmateurWorkStudent data,
                                                                     Page page) {
        try {

            int c = communityAmateurWorkStudentMapper.queryPageListcount(data);
            int totalRecords = 0;
            totalRecords = (int) c;
            PaginationBean<CommunityAmateurWorkStudent> pageHolder = new PaginationBean<CommunityAmateurWorkStudent>(
                page, totalRecords);
            List<CommunityAmateurWorkStudent> resultList = communityAmateurWorkStudentMapper
                .queryPageList(data,
                    new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                        pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#validation(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public int validation(CommunityAmateurWorkStudent data) {
        try {
            return communityAmateurWorkStudentMapper.validation(data);
        } catch (Exception e) {
            e.printStackTrace();
            return -99;
        }
    }

}
